<template>
  <el-dialog
      top="10vh"
      width="500px"
      title="添加/编辑"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />
  </el-dialog>
</template>

<script>
import { http } from "@/api";
import {CommonUserLevelEnum} from "@/utils/enum";

export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'distribution',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        rate: 100,
        user_type: CommonUserLevelEnum.BUSINESS,
        description: '',
        user_id: ''
      },
      rules: {
        rate: [{required: true, message: '分销比例不能为空'}],
        user_type: [{required: true, message: '用户类型不能为空'}],
        user_id: [{required: true, message: '请选择用户'}]
      },
      formList: [
        {
          label: '用户类型',
          prop: 'user_type',
          name: 'select',
          clearable: false,
          colSpan: 24,
          options: [
            { label: '商家', value: CommonUserLevelEnum.BUSINESS },
            { label: '一级分销商', value: CommonUserLevelEnum.DISTRIBUTOR_LEVEL1 },
            { label: '二级分销商', value: CommonUserLevelEnum.DISTRIBUTOR_LEVEL2 },
            { label: '普通用户', value: CommonUserLevelEnum.NORMAL_USER },
          ]
        },
        {
          label: '选择用户',
          prop: 'user_id',
          name: 'select',
          colSpan: 24,
          options: [],
          filterable: true,
        },
        {
          label: '下级分销折扣',
          prop: 'rate',
          name: 'inputNumber',
          colSpan: 24,
          min: 1,
          max: 100,
          step: 1,
          precision: 0,
          controlsPosition: 'right',
          append: '%'
        },
        {
          label: '描述',
          prop: 'description',
          name: 'input',
          type: 'textarea',
          colSpan: 24,
          rows: 3,
          placeholder: '请输入描述'
        }
      ]
    }
  },
  methods: {
    handleFormChange({key, val}) {
      // Handle form changes if needed
      if (key === 'user_type') {
        this.ruleForm.user_id = ''
        this.getUserList()
      }
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data,
          }
          this.getUserList()
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
        this.getUserList()
      }
    },
    getUserList() {
      if (!this.ruleForm.user_type) {
        return
      }
      http.user.list({
        user_type: this.ruleForm.user_type
      }).then(res => {
        this.formList[1].options = res.data.map(item => {
          return {
            value: item.id,
            label: item.nickname
          }
        })
      })
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, {
        ...this.ruleForm,
      }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
